package com.freesky.biz.activity.controller;

import com.freesky.biz.activity.domain.TActivityConfig;
import com.freesky.biz.activity.domain.TAlipayActivityConfigExport;
import com.freesky.biz.activity.domain.TStockMonitor;
import com.freesky.biz.activity.service.IAlipayActivityConfigService;
import com.freesky.biz.activity.service.IStockMonitorService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.annotation.Log;
import com.freesky.common.core.controller.BaseController;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.page.TableDataInfo;
import com.freesky.common.enums.BusinessType;
import com.freesky.common.utils.StringUtils;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/activity/alipay/monitor")
public class AlipayActivityConfigController extends BaseController {

    @Autowired
    private IAlipayActivityConfigService alipayActivityConfigService;

    /**
     * 获取支付宝券的监控列表
     */
    @PreAuthorize("@ss.hasPermi('activity:alipaymonitor:list')")
    @GetMapping("/list")
    public TableDataInfo list(TAlipayActivityConfigExport alipayActivityConfigExport) {
        startPage();
        List<TAlipayActivityConfigExport> list = alipayActivityConfigService.findList(alipayActivityConfigExport);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('activity:alipaymonitor:list')")
    @GetMapping("/summary")
    public AjaxResult summary(TAlipayActivityConfigExport alipayActivityConfigExport) {
        return AjaxResult.success(alipayActivityConfigService.selectSummaryData(alipayActivityConfigExport));
    }

    @Log(title = "支付宝活动状态", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('activity:alipaymonitor:export')")
    @GetMapping("/export")
    public AjaxResult export(TAlipayActivityConfigExport alipayActivityConfigExport,
                             HttpServletResponse response) {
        return alipayActivityConfigService.exportFile(alipayActivityConfigExport);
    }

    /**
     * 新增活动配置
     */
    @PreAuthorize("@ss.hasPermi('activity:alipaymonitor:add')")
    @Log(title = "支付宝活动状态", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody TAlipayActivityConfigExport alipayActivityConfigExport) {
        alipayActivityConfigExport.setId(alipayActivityConfigExport.getId().trim());
        alipayActivityConfigExport.setActivityType(alipayActivityConfigExport.getActivityType().trim());
        if(StringUtils.isEmpty(alipayActivityConfigExport.getId())){
            return AjaxResult.error("券号不能为空!");
        }
        if(StringUtils.isEmpty(alipayActivityConfigExport.getActivityType())){
            return AjaxResult.error("券类型不能为空!");
        }
        if(alipayActivityConfigService.get(alipayActivityConfigExport.getId())!=null){
            return AjaxResult.error("该券已存在!");
        }
        return toAjax(alipayActivityConfigService.insert(alipayActivityConfigExport));
    }

    /**
     * 删除活动配置
     */
    @PreAuthorize("@ss.hasPermi('activity:alipaymonitor:remove')")
    @Log(title = "支付宝活动状态", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public AjaxResult remove(@PathVariable String[] configIds) {
        alipayActivityConfigService.deleteByIds(configIds);
        return success();
    }

}
